package com.ccut;

import static com.ccut.Constant.ShapeType.TRAPEZOID;

public class Trapezoid extends MyShape {

    private double a;
    private double b;
    private double h;

    public Trapezoid() {
    }

    public Trapezoid(double a, double b, double h) {
        super(TRAPEZOID);
        this.a = a;
        this.b = b;
        this.h = h;
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this);
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    @Override
    public double calculatePerimeter() {
        return a+b+Math.sqrt(Math.pow(a-b, 2)+Math.pow(h, 2))+Math.sqrt(Math.pow(a-b, 2)+Math.pow(h, 2));
    }

    @Override
    public double calculateArea() {
        return (a+b)/2*h;
    }

    @Override
    public String toString() {
        return "图形的类型:"+ getShapeType().getChineseName() + "\n" +
                "图形的上底和下底分别为:"+ a + ", " + b + "\n" +
                "图形的高为:"+ h + "\n" +
                "图形的周长(近似):"+ String.format("%.2f", getPerimeter()) + "\n" +
                "图形的面积(近似):"+ String.format("%.2f", getArea());
    }

    @Override
    public void showShape() {
        javax.swing.SwingUtilities.invokeLater(() -> {
            javax.swing.JFrame frame = new javax.swing.JFrame("梯形绘制");
            frame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);

            javax.swing.JPanel panel = new javax.swing.JPanel() {
                @Override
                protected void paintComponent(java.awt.Graphics g) {
                    super.paintComponent(g);
                    java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;

                    // 设置抗锯齿
                    g2d.setRenderingHint(
                            java.awt.RenderingHints.KEY_ANTIALIASING,
                            java.awt.RenderingHints.VALUE_ANTIALIAS_ON
                    );

                    int width = getWidth();
                    int height = getHeight();

                    // 计算梯形顶点坐标
                    int centerX = width / 2;
                    int centerY = height / 2;

                    // 设定梯形绘制尺寸比例
                    double scale = Math.min(width, height) / 20.0;
                    int scaledA = (int) (a * scale);
                    int scaledB = (int) (b * scale);
                    int scaledH = (int) (h * scale);

                    // 保证梯形在可视范围内
                    int maxWidth = Math.max(scaledA, scaledB);
                    int displayWidth = Math.min(maxWidth, (int)(width * 0.8));
                    int displayHeight = Math.min(scaledH, (int)(height * 0.8));

                    // 重新计算比例
                    double ratio = (double)displayWidth / maxWidth;
                    int finalA = (int) (scaledA * ratio);
                    int finalB = (int) (scaledB * ratio);
                    int finalH = (int) (scaledH * ratio);

                    // 梯形四个顶点坐标
                    int[] xPoints = {
                            centerX - finalA/2,           // 上底左端点x
                            centerX + finalA/2,           // 上底右端点x
                            centerX + finalB/2,           // 下底右端点x
                            centerX - finalB/2            // 下底左端点x
                    };

                    int[] yPoints = {
                            centerY - finalH/2,           // 上底y坐标
                            centerY - finalH/2,           // 上底y坐标
                            centerY + finalH/2,           // 下底y坐标
                            centerY + finalH/2            // 下底y坐标
                    };

                    // 绘制梯形
                    g2d.setColor(java.awt.Color.GREEN);
                    g2d.drawPolygon(xPoints, yPoints, 4);

                    // 填充梯形
                    g2d.setColor(new java.awt.Color(0, 255, 0, 50));
                    g2d.fillPolygon(xPoints, yPoints, 4);

                    // 绘制信息文本 - 使用支持中文的字体
                    g2d.setColor(java.awt.Color.BLACK);
                    // 修改为支持中文的字体
                    java.awt.Font font = new java.awt.Font("微软雅黑", java.awt.Font.PLAIN, 12);
                    g2d.setFont(font);
                    g2d.drawString("梯形 (上底: " + String.format("%.2f", a) +
                            ", 下底: " + String.format("%.2f", b) +
                            ", 高: " + String.format("%.2f", h) + ")", 20, 20);
                    g2d.drawString("周长: " + String.format("%.2f", getPerimeter()), 20, 40);
                    g2d.drawString("面积: " + String.format("%.2f", getArea()), 20, 60);
                }
            };

            frame.add(panel);
            frame.setSize(500, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}
